package cn.edu.scau.cmi.ema.controller;

import java.util.Set;
import java.util.List;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;
import cn.edu.scau.cmi.ema.service.pagination.*;
import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;

@Controller("FiletypeController")
public class FiletypeController extends CmiEmaController {

	@RequestMapping("/listFiletypes")
	public ModelAndView listFiletypes(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = filetypePaginationService.updatePagination(request, pagination);
		Set<Filetype> filetypes = filetypePaginationService.getPaginationFiletypes(pagination);

		mav.addObject("filetypes", filetypes);
		mav.addObject("entityName", "filetype");
		mav.addObject("pagination", pagination);
		mav.setViewName("filetype/listFiletypes.jsp");
		return mav;
	}

	public String listFiletypes() {
		return "redirect:/listFiletypes";
	}

	@RequestMapping("/viewFiletype")
	public ModelAndView viewFiletype(@RequestParam Integer filetypeId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("filetype", filetypeDAO.findFiletypeByPrimaryKey(filetypeId));
		mav.setViewName("filetype/viewFiletype.jsp");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteFiletypes")
	public ModelAndView confirmDeleteFiletypes(
			@RequestParam(value = "deleteFiletypesIds", required = true) Set<Integer> deleteFiletypesIds) {
		ModelAndView mav = new ModelAndView();

		Set<Filetype> deleteFiletypes = filetypeDAO.findFiletypesByIds(deleteFiletypesIds);
		Set<Filetype> referencedFiletypes = filetypeService.loadReferenceFiletypes(deleteFiletypes);
		boolean hasReferenced;
		if (referencedFiletypes.size() == 0) {
			mav.addObject("deleteEntitys", deleteFiletypes);
			hasReferenced = false;
		} else {
			deleteFiletypes.removeAll(referencedFiletypes);
			mav.addObject("referencedEntitys", referencedFiletypes);
			mav.addObject("deleteEntitys", deleteFiletypes);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Filetype");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listFiletypes");
		return mav;
	}

	@RequestMapping("/deleteFiletype")
	public String deleteFiletype(@RequestParam Integer filetypeId) {
		Filetype filetype = filetypeDAO.findFiletypeByPrimaryKey(filetypeId);
		filetypeService.deleteFiletype(filetype);
		return "forward:/listFiletypes";
	}

	@RequestMapping("/confirmDeleteFiletype")
	public ModelAndView confirmDeleteFiletype(@RequestParam Integer filetypeId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("filetype", filetypeDAO.findFiletypeByPrimaryKey(filetypeId));

		mav.setViewName("filetype/deleteFiletype.jsp");

		return mav;
	}

	@RequestMapping("/deleteFiletypes")
	public String deleteFiletypes(
			@RequestParam(value = "deleteFiletypesId", required = true) Set<Integer> deleteFiletypesId) {
		filetypeService.deleteFiletypes(deleteFiletypesId);
		return "forward:/listFiletypes";
	}

	@RequestMapping("/editFiletype")
	public ModelAndView editFiletype(@RequestParam Integer filetypeId) {

		ModelAndView mav = new ModelAndView();

		Filetype filetype = filetypeDAO.findFiletypeByPrimaryKey(filetypeId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("filetype", filetype);

		// 把所有的引用filetype对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(filetypeService.loadFiletypeObjectReferencedSet(filetype));

		// 将编辑页面filetype添加到mav中
		mav.setViewName("filetype/editFiletype.jsp");
		return mav;
	}

	@RequestMapping("/newFiletype")
	public ModelAndView newFiletype() {
		ModelAndView mav = new ModelAndView();
		Filetype filetype = new Filetype();

		mav.addObject("filetype", filetype);

		// 把所有的引用filetype对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(filetypeService.loadFiletypeObjectReferencedSet(filetype));

		mav.addObject("newFlag", true);
		mav.setViewName("filetype/editFiletype.jsp");

		return mav;
	}

	@RequestMapping("/saveFiletype")
	public ModelAndView saveFiletype(@ModelAttribute Filetype filetype) {

		ModelAndView mav = new ModelAndView();

		Filetype existingFiletype = CmiHibernateProxyUtil
				.deproxy(filetypeDAO.findFiletypeByPhysicalPrimaryKey(filetype.getName()));

		// 新建记录
		if (filetype.getId() == null) {
			if (filetype.equals(existingFiletype)) {

				mav.addAllObjects(filetypeService.loadFiletypeClassReferencedSet(filetype));
				// mav = editContinue(mav, filetype);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("filetype/editFiletype.jsp");
			} else {
				filetypeService.saveFiletype(filetype);
				mav.setViewName("forward:/listFiletypes");
			}
		}
		// 编辑记录
		else {
			Filetype originFiletype = CmiHibernateProxyUtil
					.deproxy(filetypeDAO.findFiletypeByPrimaryKey(filetype.getId()));
			if (!filetype.equals(originFiletype) && filetype.equals(existingFiletype)) {
				mav.addAllObjects(filetypeService.loadFiletypeClassReferencedSet(filetype));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("filetype/editFiletype.jsp");
			} else {
				filetypeService.saveFiletype(filetype);
				mav.setViewName("forward:/listFiletypes");
			}
		}
		return mav;
	}

	@RequestMapping("/filetypeController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
